﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Distribution.Controllers
{
    public class CategoryController : GeneralController
    {
        private ICategoryService _iCategoryService;
        private IMerchantService _iMerchantService;

        public CategoryController(ICategoryService iCategoryService,IMerchantService iMerchantService) {
            _iCategoryService = iCategoryService;
            _iMerchantService = iMerchantService;
        }

        [HttpGet]
        public JsonResult Get(long id) {
            var result = _iCategoryService.Get(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Gets() {
            var merchant = _iMerchantService.Get(currentUser.user_id);
            var result = _iCategoryService.GetAll(currentUser.user_id, 1);
            var categorys = new List<category_ex>();
            foreach (var item in result) {
                categorys.Add(new category_ex { id = item.id, parent_id = item.parent_id, image_urls = item.image_urls, name = item.name, remark = item.remark, sort = item.sort, depth = item.depth });
                foreach (var child in item.children) {
                    categorys.Add(new category_ex { id = child.id, parent_id = child.parent_id, image_urls = child.image_urls, name = child.name, remark = child.remark, sort = child.sort, depth = child.depth });
                }
            }
            return Json(ResultToJson.Json(categorys), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [ValidateInput(false)]
        public JsonResult Save(category_model model) {
            var merchant = _iMerchantService.Get(currentUser.user_id);
            model.bind_no = merchant.merchant_no;
            model.image_urls = UploadList("goods", merchant.merchant_no);
            var result = _iCategoryService.Save(model);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpDelete]
        public JsonResult Delete(long id) {
            var result = _iCategoryService.DeleteCategory(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetOems() {
            var merchant = _iMerchantService.Get(currentUser.user_id);
            var result = _iCategoryService.GetAll(merchant.oem_id.Value);
            var categorys = new List<category_ex>();
            foreach (var item in result) {
                categorys.Add(new category_ex { id = item.id, parent_id = item.parent_id, image_urls = item.image_urls, name = item.name, remark = item.remark, sort = item.sort, depth = item.depth });
                foreach (var child in item.children) {
                    categorys.Add(new category_ex { id = child.id, parent_id = child.parent_id, image_urls = child.image_urls, name = child.name, remark = child.remark, sort = child.sort, depth = child.depth });
                }
            }
            return Json(ResultToJson.Json(categorys), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [ValidateInput(false)]
        public JsonResult SaveOem(category_model model) {
            var merchant = _iMerchantService.Get(currentUser.user_id);
            var oemer = _iMerchantService.Get(merchant.oem_id.Value);
            model.image_urls = UploadList("goods", oemer.merchant_no);
            model.bind_no = oemer.merchant_no;
            var result = _iCategoryService.Save(model);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}